package com.bjtang.book.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjtang.book.dto.IdDto;
import com.bjtang.book.dto.ReturnDto;
import com.bjtang.book.entiy.AttentionTable;
import com.bjtang.book.entiy.ShareTable;
import com.bjtang.book.myexception.DataCheckException;
import com.bjtang.book.service.AttentionService;
import com.bjtang.book.service.MyService;
import com.bjtang.book.service.ShareService;
import com.bjtang.book.util.ReturnUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "文章分享综合管理")
@Validated
@RestController
@RequestMapping(value = "/share/")
public class ShareController {
	@Autowired
	ShareService shareService;
	@Autowired
	AttentionService attentionService;

	@Autowired
	private MyService myService;

	@ApiOperation("新建分享")
	@PostMapping("/")
	public ReturnDto addShare(@RequestBody @Valid ShareTable share) {
		// 注入创建人
		share.setFounder(myService.getMyselfId());
		return ReturnUtil.success(shareService.save(share));
	}

	@ApiOperation("删除指定分享")
	@DeleteMapping("/")
	public ReturnDto delArticle(@RequestBody @Valid IdDto shareId) throws Exception {
		ShareTable byId = shareService.getById(shareId.getId());
		if (byId == null)
			throw new DataCheckException("分享没找到");
		if (byId.getFounder().longValue() != myService.getMyselfId().longValue())
			throw new DataCheckException("没有操作权限");
		return ReturnUtil.successOrError(shareService.removeById(shareId.getId()), shareId.getId());
	}

	@ApiOperation("查询分享ByUserPage")
	@GetMapping("/by-user/{userId}/{current}/{size}")
	public ReturnDto GetArticleByUser(@PathVariable Long userId, @PathVariable Long current, @PathVariable Long size) {
		Page<ShareTable> page = new Page<ShareTable>(current, size);
		return ReturnUtil.success(shareService.lambdaQuery().eq(ShareTable::getFounder, userId)
				.orderByDesc(ShareTable::getShareNumber).page(page));
	}

	@ApiOperation("查询分享ByUserFriendPage")
	@GetMapping("/by-friend/{current}/{size}")
	public ReturnDto GetArticleByUserList(@PathVariable Long current, @PathVariable Long size) {
		// 获取登录用户的关注列表
		List<AttentionTable> list = attentionService.lambdaQuery()
				.eq(AttentionTable::getFounder, myService.getMyselfId()).list();
		List<Long> userIdList = new ArrayList<Long>();
		list.forEach(e -> userIdList.add(e.getUserNumber()));
		userIdList.add(0L);
		// 通过in的方式获取分享列表
		Page<ShareTable> page = new Page<ShareTable>(current, size);
		return ReturnUtil.success(shareService.lambdaQuery().in(ShareTable::getFounder, userIdList)
				.orderByDesc(ShareTable::getShareNumber).page(page));
	}
}
